<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/7 15:12
 */

namespace App\Project\Printer;

use App\Models\CnDiagnose;
use App\Models\MedicalDiagnose;
use App\Models\MedicalRecord;

class DiagnosePrinter extends PrinterAbstract
{
    protected $record;

    public function __construct(MedicalDiagnose $diagnose, MedicalRecord $record)
    {
        $this->model = $diagnose;
        $this->record = $record;
    }

    public function diagnose()
    {
        $diagnose = $this->model->diagnose;

        if (!$diagnose->count()) {
            return null;
        }

        $print = [
            $diagnose->pluck('name')->map(function ($name) {
                return '<p style="text-indent:1em">' . $name . ';</p>';
            })->implode('')
        ];

        $isWestern = app('scene')->isWestern();
        $left = '';
        if (!$isWestern) {
            $left = '<p class="caseTitle">西医诊断</p>';
        }

        return $this->getPrintStr($left, $print, '');
    }

    public function cndiagnoses()
    {
        if ($this->record->isWestern()) {
            return '';
        }

        $cnDiagnoses = $this->model->cnDiagnoses;

        if (!$cnDiagnoses) {
            return '';
        }

        $print = [];

        $cn_diagnose = app('field')->get('diagnose.cn_diagnose');

        collect($cnDiagnoses)->map(function ($cnDiagnose, $key) use (&$print, $cn_diagnose) {

            $print[$key + 1] = '<p style="text-indent:1em">' . $cnDiagnose['disease']['name'] . '</p>';

            $print[$key + 1] .= '<p style="text-indent:2em">' . $cn_diagnose['type'][$cnDiagnose['type_id']];
            if (array_key_exists('carry_key', $cnDiagnose))
                if ($with = array_intersect_key($cn_diagnose['carry'], array_flip($cnDiagnose['carry_key']))) {
                    $print[$key + 1] .= '&nbsp;&nbsp;挟:' . implode('、', $with);
                }

            $print[$key + 1] .= ';</p>';
        });

        return $this->getPrintStr('<p class="caseTitle">中医诊断</p>', $print, '', '');
    }
}